package com.example.myapplication8;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication8.databinding.ActivityInterestBinding;
import java.util.ArrayList;
import java.util.List;

public class InterestActivity extends AppCompatActivity {
    private ActivityInterestBinding binding;
    private User user; // 接收登录页传递的用户信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInterestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 1. 获取登录页传递的User对象
        Intent intent = getIntent();
        if (intent != null) {
            user = (User) intent.getSerializableExtra("USER_INFO");
        }

        // 2. 确认按钮点击事件
        binding.btnConfirm.setOnClickListener(v -> confirmInterest());
    }

    // 收集选中的兴趣并跳转主页
    private void confirmInterest() {
        List<String> selectedInterests = new ArrayList<>();

        // 检查哪些兴趣被选中
        if (binding.cbSports.isChecked()) {
            selectedInterests.add("运动");
        }
        if (binding.cbReading.isChecked()) {
            selectedInterests.add("阅读");
        }
        if (binding.cbMusic.isChecked()) {
            selectedInterests.add("音乐");
        }
        if (binding.cbTravel.isChecked()) {
            selectedInterests.add("旅行");
        }

        // 验证：至少选择1个兴趣
        if (selectedInterests.isEmpty()) {
            Toast.makeText(this, "请至少选择一个兴趣", Toast.LENGTH_SHORT).show();
            return;
        }

        // 3. 将选中的兴趣存入User对象，传递给主页
        user.setSelectedInterests(selectedInterests);
        Intent toMainIntent = new Intent(this, MainActivity.class);
        toMainIntent.putExtra("USER_INFO", user); // 传递用户+兴趣信息
        startActivity(toMainIntent);
        finish(); // 关闭兴趣页，避免返回
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}